let reg_phone = /^ (13 [0-9]|14 |15 [0-3,5-9]|16 |17 |18 [0-9]|19)d {8}$/
let reg_password = /^[a-z\d_-]{6,12}$/
let phInput = document.querySelector('#phoneInput') //手机号的input框
let btn = document.querySelector('.btn-block')      // 登录按钮
let pwInput = document.querySelector('#passwordInput') // 密码的input框
let alerts = document.querySelector('.alert')
function ph() {
    if (phInput.value.length === 0) {
        alerts.style.display = 'block'
        alerts.innerHTML = `<i class="layui-icon layui-icon-about;">&#xe60b;</i> 手机号码不能为空`
        setTimeout(function () {
            alerts.style.display = 'none'
        },3000)
        return false
    }
    if (reg_password.test(phInput.value)) {
        alerts.style.display = 'none'
        return true
    } else {
        alerts.style.display = 'block'
        alerts.innerHTML = `<i class="layui-icon layui-icon-about;">&#xe60b;</i> 手机号码格式输入错误`
        setTimeout(function () {
            alerts.style.display = 'none'
        },3000)
        return false
    }
}

function pw() {
    if (pwInput.value.length === 0) {
        alerts.style.display = 'block'
        alerts.innerHTML=`<i class="layui-icon layui-icon-about;">&#xe60b;</i> 密码不能为空`
        setTimeout(function () {
            alerts.style.display = 'none'
        },3000)
        return false
    }
    if (reg_password.test(pwInput.value)) {
        alerts.style.display = 'none'
        return true
    } else {
        alerts.style.display = 'block'
        alerts.innerHTML = `<i class="layui-icon layui-icon-about;">&#xe60b;</i> 密码格式输入错误`
        setTimeout(function () {
            alerts.style.display = 'none'
        },3000)
        return false
    }
}

btn.addEventListener('click', function () {
    pw()
    ph()
    if (pw() === false || ph() === false) {
        console.log(111)
        return 
    }
    axios({
        method: 'POST',
        url: 'http://124.223.14.236:8060/home/index/login',
        data: {
            mobile:phoneInput.value,
            password:passwordInput.value,
        }   

    }).then(res => {
        if (res.data.errno === 1000) {
            alerts.style.display = 'block'
            alerts.innerHTML = `<i class="layui-icon layui-icon-close-fill;">&#x1007;</i> 账号不存在`
            setTimeout(function () {
                alerts.style.display = 'none'
            },3000)
        } 
        location.href = './user.html'
      }).catch(err => {
        alert('网络不好')
    })
})
// btn.addEventListener('click', function () {
//     if (ph() == true & pw() == true) {
//         console.log(111)
//     }
// })
